package org.ade.monak.anak.boundary;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import org.ade.monak.anak.Variable.Status;
import org.ade.monak.anak.entity.Anak;
import org.ade.monak.anak.entity.DataMonitoring;
import org.ade.monak.anak.entity.DateMonitoring;
import org.ade.monak.anak.entity.DayMonitoring;
import org.ade.monak.anak.entity.Lokasi;
import org.ade.monak.anak.entity.Ortu;
import org.ade.monak.anak.service.storage.DatabaseManagerAnak;
import org.ade.monak.anak.util.BundleEntityMaker;
import org.ade.monak.anak.util.EntityBundleMaker;
import org.ade.monak.anak.util.IDGeneratorAnak;
import org.ade.monak.anak.R;

import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PendaftaranMonitoring extends Activity{

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.pendaftaran_monitoring);
		
		isEdit = getIntent().getBooleanExtra(EXTRA_EDIT, false);
		
		justView = getIntent().getBooleanExtra(EXTRA_JUST_VIEW, false);
	
		databaseManager = new DatabaseManagerAnak(this);
		dataMonitoring 	= EntityBundleMaker.getDataMonitoringFromBundle(getIntent().getExtras());
		mIDGenerator	= new IDGeneratorAnak(this,databaseManager);
		
		String idMonitoring = "";
		if(dataMonitoring==null){
			dataMonitoring = new DataMonitoring();
			
			idMonitoring = mIDGenerator.getIdMonitoring();	
		}else{
			idMonitoring = dataMonitoring.getIdMonitoring();
		}

		TextView textId= (TextView) findViewById(R.id.monitoringTextId);
		textId.setText(idMonitoring);
		dataMonitoring.setIdMonitoring(idMonitoring);
		
		Bundle bundle = getIntent().getExtras();
		Anak anak = EntityBundleMaker.getAnakFromBundle(bundle);
		if(anak!=null){
			dataMonitoring.setAnak(anak);
			TextView txt = (TextView) findViewById(R.id.monitoringTextAnak);
			txt.setText(anak.getNamaAnak());
		}

		if(justView){
			Button buttonOk 	= (Button) findViewById(R.id.monitoringButtonOk);
			Button buttonClear = (Button) findViewById(R.id.monitoringButtonClear);
			buttonOk.setEnabled(false);
			buttonClear.setEnabled(false);
		}else{
			initAllButton();
		}
		
		initSubMenu();
		
		if(isEdit){

			String idOrtu = getIntent().getStringExtra("idOrtu");
			dataMonitoring = databaseManager.getDataMonitoringByIdMonitoringAndOrtu(dataMonitoring.getIdMonitoring(), idOrtu, true, true);
			initEditMode();
		}
		

	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
	
		switch(requestCode){
			case LOKASI:{
				if(resultCode==RESULT_OK && data!=null){
				
					Lokasi lokasi = new Lokasi();
					lokasi.setId(mIDGenerator.getIdLocation());
					lokasi.setLatitude(data.getDoubleExtra("latitude", 0));
					lokasi.setLongitude(data.getDoubleExtra("longitude", 0));
					dataMonitoring.setLokasi(lokasi);
					
					TextView textLokasi = (TextView) findViewById(R.id.monitoringTextLokasi);
					textLokasi.setText(lokasi.getlatitude()+", "+lokasi.getLongitude());
				}
				break;
			}
		}
	}
	
	private void initSubMenu(){
		
	}

	private void initAllButton(){
//		LinearLayout buttonAnak 		= (LinearLayout) findViewById(R.id.monitoringButtonAnak);
//		buttonAnak.setOnClickListener(new OnClickListener() {
//			
//			public void onClick(View v) {actionPilihAnak();}
//		}); ditutup karena sudah ada anak otomatis...
		
		LinearLayout buttonStatus		= (LinearLayout) findViewById(R.id.monitoringButtonStatus);
		buttonStatus.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {actionPilihStatus();}
		});
		
		LinearLayout buttonKeterangan	= (LinearLayout) findViewById(R.id.monitoringButtonKeterangan);
		buttonKeterangan.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {actionPilihKeterangan();}
		});
		
		LinearLayout buttonWaktu 		= (LinearLayout) findViewById(R.id.monitoringButtonWaktu);
		buttonWaktu.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {actionPilihWaktu();}
		});
		
		LinearLayout buttonMingguan 	= (LinearLayout) findViewById(R.id.monitoringButtonMingguan);
		buttonMingguan.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {actionPilihMingguan();}
		});
		
		LinearLayout buttonTanggal 		= (LinearLayout) findViewById(R.id.MonitoringButtonTanggal);
		buttonTanggal.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {actionPilihTanggal();}
		});
		
		LinearLayout buttonLokasi 		= (LinearLayout) findViewById(R.id.monitoringButtonLokasi);
		buttonLokasi.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {actionPilihLokasi();}
		});
		
		LinearLayout buttonToleransi 	= (LinearLayout) findViewById(R.id.monitoringButtonToleransi);
		buttonToleransi.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {actionPilihToleransi();}
		});
		
		Button buttonOk 	= (Button) findViewById(R.id.monitoringButtonOk);
		buttonOk.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {			
			}
		});
		
		Button buttonClear = (Button) findViewById(R.id.monitoringButtonClear);
		buttonClear.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				actionClear();
			}
		});
	}
	
	private void initEditMode(){
		if(dataMonitoring!=null){
			Ortu ortu = dataMonitoring.getOrtu();
			if(ortu!=null){
				TextView txt = (TextView) findViewById(R.id.monitoringTextAnak);
				txt.setText(ortu.getIdOrtu());
			}
			
			if(dataMonitoring.getWaktuMulai()!=0){
				List<Long> waktus = new ArrayList<Long>();
				long waktu1 = dataMonitoring.getWaktuMulai();
				long waktu2 = dataMonitoring.getWaktuSelesai();
				waktus.add(waktu1);
				waktus.add(waktu2);
				
				TextView textWaktu = (TextView) findViewById(R.id.monitoringTextWaktu);
				Calendar cal = Calendar.getInstance();
				cal.setTimeInMillis(waktus.get(0));
				String stringWaktu = cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE)+" s/d ";
				cal.setTimeInMillis(waktus.get(1));
				stringWaktu += cal.get(Calendar.HOUR_OF_DAY)+":"+cal.get(Calendar.MINUTE);
				textWaktu.setText(stringWaktu);
				
			}
			
			List<DayMonitoring> haris = dataMonitoring.getHaris();
			if(haris!=null && haris.size()>0){
				List<Integer> angkas = new ArrayList<Integer>();
				for(DayMonitoring day:haris){
					angkas.add(day.getHari());
				}
				TextView textHaris = (TextView) findViewById(R.id.monitoringTextMingguan);
				String stringHari = "";
				for(int hari:angkas){
					stringHari += hari+",";
				}
				if(!stringHari.equals("")){
					stringHari = stringHari.substring(0, stringHari.length()-1);	
				}
				textHaris.setText(stringHari);
			}
			
			int toleransi = dataMonitoring.getTolerancy();
			if(toleransi>0){
				TextView textToleransi = (TextView) findViewById(R.id.monitoringTextToleransi);
				textToleransi.setText(toleransi+"m");
			}
			
			TextView textKeterangan = 
					(TextView) findViewById(R.id.monitoringTextKeterangan);
	    	textKeterangan.setText(dataMonitoring.getKeterangan());
	    	
	    	
	    	List<DateMonitoring> tanggals = dataMonitoring.getTanggals();
	    	if(tanggals!=null && tanggals.size()>0){
	    		
	    		Calendar cal = Calendar.getInstance();
	    		
	    		DateMonitoring tanggal = tanggals.get(tanggals.size()-1);
				long timeMillis = tanggal.getDate();
				cal.setTimeInMillis(timeMillis);
				
	    		TextView tv = (TextView) findViewById(R.id.monitoringTextTanggal);
	    		tv.setText(cal.get(Calendar.DATE)+"-"+
						(cal.get(Calendar.MONTH)+1)+"-"+
							cal.get(Calendar.YEAR));
	    		
	    	}
	    	
	    	int status = dataMonitoring.getStatus();
	    	
	    	TextView textStatus= (TextView) findViewById(R.id.monitoringTextStatus);
			if(status== DataMonitoring.SEHARUSNYA){
				textStatus.setText("Seharusnya");
			}else{
				textStatus.setText("Terlarang");
			}
			
			Lokasi lokasi = dataMonitoring.getLokasi();
			if(lokasi!=null){
				TextView textLokasi = (TextView) findViewById(R.id.monitoringTextLokasi);
				textLokasi.setText(lokasi.getlatitude()+", "+lokasi.getLongitude());
			}
	    	
		}
	}
	
//	private void actionPilihAnak(){
//		showDialog(ANAK);
//	}
	
	private void actionPilihStatus() {
		showDialog(STATUS_LOKASI);
	}
	
	private void actionPilihKeterangan() {
		showDialog(KETERANGAN);
	}
	
	private void actionPilihWaktu(){
		showDialog(WAKTU);
	}
	
	private void actionPilihMingguan(){
		showDialog(MINGGUAN);
	}
	
	private void actionPilihTanggal(){
		showDialog(TANGGAL);
	}
	
	private void actionPilihLokasi(){
		showDialog(LOKASI);
	}
	
	private void actionPilihToleransi(){
		showDialog(TOLERANSI);
	}
	
	
	
	private void save(){
		
	}
	
	private void actionClear(){
		dataMonitoring = new DataMonitoring();
		startActivity(getIntent()); 
		finish();
		
	}	

	@Override
	protected Dialog onCreateDialog(int id) {
		return null;
		
	}
	
	
	public DataMonitoring getDataMonitoring() {
		return dataMonitoring;
	}

	public void setDataMonitoring(DataMonitoring dataMonitoring) {
		this.dataMonitoring = dataMonitoring;
	}

	private DatabaseManagerAnak databaseManager;

	private IDGeneratorAnak		mIDGenerator;
	
	private DataMonitoring dataMonitoring;
	
	private boolean isEdit;
	private boolean justView;
	
	public final static String EXTRA_EDIT		= "isEdit";
	public final static String EXTRA_JUST_VIEW	= "justView";
	
	public final static int ANAK			= 0;
	public final static int KETERANGAN		= 1;
	public final static int WAKTU			= 2;
	public final static int TANGGAL			= 3;
	public final static int MINGGUAN		= 4;
	public final static int STATUS_LOKASI	= 5;
	public final static int TOLERANSI		= 6;
	public final static int LOKASI			= 7;
	public final static int KE_ANAK			= 8;
	
	private final static class HandlerSendermonitoring extends Handler{

		public HandlerSendermonitoring(PendaftaranMonitoring pendaftaranMonitoring){
			this.pendaftaranMonitoring = pendaftaranMonitoring;
		}
		@Override
		public void handleMessage(Message msg) {
			switch(msg.what){
				case Status.SUCCESS:{
					pendaftaranMonitoring.save();
					break;
				}case Status.FAILED:{
					break;
				}
			}
		}
		private PendaftaranMonitoring pendaftaranMonitoring;
	}
	
}